﻿using System;
using System.Collections.Generic;
using System.Linq;
using RimWorld;
using Verse;
using Verse.AI;

namespace ARMIR
{
    public class HediffComp_abilitySet : HediffComp {
        public HediffCompProperties_abilitySet Props => (HediffCompProperties_abilitySet)props;
        private int gapAbilityCheck;
        public override void CompPostMake()
        {
            base.CompPostMake();
            gapAbilityCheck = 0;
        }
        public override void CompPostTick(ref float severityAdjustment)
        {
            base.CompPostTick(ref severityAdjustment);
            gapAbilityCheck++;
            if (gapAbilityCheck > 30) {
                abilitySet();
                dropTacticMateriel();
                gapAbilityCheck = 0;
            }
            
        }
        public override void CompPostPostAdd(DamageInfo? dinfo)
        {
            if (Props.constantAbilityA != null)
            {
                parent.pawn.abilities.GainAbility(Props.constantAbilityA);
            }
            if (Props.constantAbilityB != null)
            {
                parent.pawn.abilities.GainAbility(Props.constantAbilityB);
            }
        }

        public override void CompPostPostRemoved()
        {
            if (Props.constantAbilityA != null)
            {
                parent.pawn.abilities.RemoveAbility(Props.constantAbilityA);
            }
            if (Props.constantAbilityB != null)
            {
                parent.pawn.abilities.RemoveAbility(Props.constantAbilityB);
            }
        }
        /// <summary>
        ///  检查hediff持有者的状态以管理ability
        /// </summary>
        private void abilitySet() {
            bool hasRequireEquipment = false;
            if (parent.pawn.equipment.AllEquipmentListForReading.Any(e => e.def == Props.requireEquipment)
                || parent.pawn.apparel.WornApparel.Any(a => a.def == Props.requireEquipment))
            { hasRequireEquipment = true; }
            List<Ability> abilities = parent.pawn.abilities.abilities;
            bool hasRequireAbilityA = false;
            bool hasRequireAbilityB = false;
            bool hasRequireAbilityC = false;
            if (abilities != null) {
                foreach (Ability ability in abilities) {
                    if (ability.def == Props.requireAbilityA) { 
                        hasRequireAbilityA = true;
                        continue;
                    }
                    if (ability.def == Props.requireAbilityB) { 
                        hasRequireAbilityB = true;
                        continue;
                    }
                    if (ability.def == Props.requireAbilityC) { 
                        hasRequireAbilityC = true;
                        continue;
                    }
                }
            }
            if (hasRequireEquipment) {
                if (!hasRequireAbilityA) {
                    parent.pawn.abilities.GainAbility(Props.requireAbilityA);
                }
                if (!hasRequireAbilityB) {
                    parent.pawn.abilities.GainAbility(Props.requireAbilityB);
                }
                if (!hasRequireAbilityC) {
                    parent.pawn.abilities.GainAbility(Props.requireAbilityC);
                }
            }
            else {
                if (hasRequireAbilityA) { 
                    parent.pawn.abilities.RemoveAbility(Props.requireAbilityA);
                }
                if (hasRequireAbilityB) { 
                    parent.pawn.abilities.RemoveAbility(Props.requireAbilityB);
                }
                if (hasRequireAbilityC) { 
                    parent.pawn.abilities.RemoveAbility(Props.requireAbilityC);
                }
            }
        }
        private void dropTacticMateriel() {
            if (parent.pawn.Downed) {
                //主体，如果迷迭香已倒下，则下掉其战术装备
                List<Apparel> apparelToRemove = new List<Apparel>();
                var apparels = parent.pawn.apparel.WornApparel;
                if (apparels != null && apparels.Count > 0) {
                    foreach (var apparel in apparels) {
                        if (apparel.def.defName == "ARMIR_Tactics_Annihilated_Materiel") { 
                            apparelToRemove.Add(apparel);
                        }
                    }
                }
                if (apparelToRemove.Count > 0) {
                    foreach (var apparel in apparelToRemove) {
                        parent.pawn.apparel.TryDrop(apparel);
                    }
                }
            }
        }
    }
}
